﻿
namespace ZquenceStudio3Host.DspPlugins.Synthesizers.Nodesizer.Engine.Modules.LowLevel
{
    [ModuleDisplay("Spread")]
    class SpreadModule : BaseModule
    {
        public readonly ModuleInput A;
        public readonly ModuleInput B;
        public readonly ModuleOutput Out1;
        public readonly ModuleOutput Out2;
        public readonly ModuleOutput Out3;
        public readonly ModuleOutput Out4;
        public double Spread { get; set; }

        public SpreadModule(ModuleManager manager)
            : base(manager)
        {
            // Add input and outputs to module.
            AddInput(A = new ModuleInput("In", IOType.AudioOrWaveform, this));
            AddInput(B = new ModuleInput("+ spread", IOType.AudioOrWaveform, this));
            AddOutput(Out1 = new ModuleOutput("1.0", IOType.AudioOrWaveform, this));
            AddOutput(Out2 = new ModuleOutput("0.5", IOType.AudioOrWaveform, this));
            AddOutput(Out3 = new ModuleOutput("-0.5", IOType.AudioOrWaveform, this));
            AddOutput(Out4 = new ModuleOutput("-1.0", IOType.AudioOrWaveform, this));

            ModuleParameter spread = new ModuleParameter(this);
            spread.Label = "Spread";
            spread.MinValue = 0;
            spread.MaxValue = 1;
            spread.DirectGet += delegate { return Spread; };
            spread.DirectSet += delegate (double value) { Spread = value; };
            AddParameter(spread);
        }

        public override void Process()
        {
            // First check if A connected.

            if (A.IsConnected && B.IsConnected)
            {
                for (int i = 0; i < Out1.AudioBuffer.Length; i++)
                {
                    Out1.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * 1.0 + A.FromOutput.AudioBuffer[i];
                    Out2.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * 0.5 + A.FromOutput.AudioBuffer[i];
                    Out3.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * -0.5 + A.FromOutput.AudioBuffer[i];
                    Out4.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * -1.0 + A.FromOutput.AudioBuffer[i];
                }
            }
            else if (A.IsConnected)
            {
                for (int i = 0; i < Out1.AudioBuffer.Length; i++)
                {
                    Out1.AudioBuffer[i] = Spread * 1.0 + A.FromOutput.AudioBuffer[i];
                    Out2.AudioBuffer[i] = Spread * 0.5 + A.FromOutput.AudioBuffer[i];
                    Out3.AudioBuffer[i] = Spread * -0.5 + A.FromOutput.AudioBuffer[i];
                    Out4.AudioBuffer[i] = Spread * -1.0 + A.FromOutput.AudioBuffer[i];
                }
            }
            else if (B.IsConnected)
            {
                for (int i = 0; i < Out1.AudioBuffer.Length; i++)
                {
                    Out1.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * 1.0;
                    Out2.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * 0.5;
                    Out3.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * -0.5;
                    Out4.AudioBuffer[i] = (B.FromOutput.AudioBuffer[i] + Spread) * -1.0;
                }
            }
            else
            {
                for (int i = 0; i < Out1.AudioBuffer.Length; i++)
                {
                    Out1.AudioBuffer[i] = Spread * 1.0;
                    Out2.AudioBuffer[i] = Spread * 0.5;
                    Out3.AudioBuffer[i] = Spread * -0.5;
                    Out4.AudioBuffer[i] = Spread * -1.0;
                }
            }
        }
    }
}
